<?php
/**
 * Created by IntelliJ IDEA.
 * User: HXL
 * Date: 2018/5/9
 * Time: 13:33
 */
namespace app\orders\controller;

use app\common\ErrorCodes;
use think\Lang;

class Manu extends Base{

    public function index(){
//        $this->authVerify();
        $field = 'mf_id,manufacturer';
        $order = '';
        $where = '';
        $manus = $this->manus()->page($field, $where, $order);
        return $this->show($manus);
        /*$this->page($manus);
        $this->assign('manus', $manus);
        return $this->fetch("manu/index");*/
    }

    /**
     * 添加action
     */
    public function save(){

        $manus = input('post.');
        $validate = $this->validate($manus, 'Manus');
        //var_dump($validate);exit();
        if(true !== $validate){
            return $this->show(array(), ErrorCodes::$FAIL, " $validate ");
//            return $this->error(" $validate ");
        }
        $result = $this->manus()->add($manus, '');
        if($result < 1){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('add fail'));
//            return $this->error(Lang::get('add fail'));
        }
        return $this->show($manus, ErrorCodes::$SUCCESS, Lang::get('add success'));
//        return $this->success(Lang::get('add success'), 'Manu/index');
    }

    /**
     * json数据
     */
    public function one(){
        $mfId = input('param.mf_id');
        $field = 'mf_id,manufacturer';
        $where = array('mf_id'=>$mfId);
        $data = $this->manus()->select($field, $where);
        return $this->show($data);
    }


    /**
     * 更新action
     */
    public function esave(){

        $mfId = input('param.mf_id');
        $field = 'mf_id,manufacturer';
        $where = array('mf_id'=>$mfId);
        $find = $this->manus()->findById($where);
        if(!$find){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('unfind manufacturer'));
//            return $this->error(Lang::get('unfind manufacturer'));
        }
        $manus = input('post.');
        $validate = $this->validate($manus, 'Manus');
        //var_dump($validate);exit();
        if(true !== $validate){
            return  $this->show(array(), ErrorCodes::$FAIL, " $validate ");
//            return $this->error(" $validate ");
        }
        $result = $this->manus()->update($manus, $where);
        if($result < 1){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('edit fail'));
//            return $this->error(Lang::get('edit fail'));
        }
        return $this->show($manus, ErrorCodes::$SUCCESS, Lang::get('edit success'));
//        return $this->success(Lang::get('edit success'), 'Manu/index');
    }


    public function del(){

        $mfId = input('param.mf_id');
        $where = array('mf_id'=>$mfId);
        $find = $this->manus()->findById($where);
        if(!$find){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('unfind manufacturer'));
//            return $this->error(Lang::get('unfind manufacturer'));
        }
        $result = $this->manus()->del($where);
        if($result < 1){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('del fail'));
//            return $this->error(Lang::get('del fail'));
        }
        return $this->show($find, ErrorCodes::$SUCCESS, Lang::get('del success'));
//        return $this->success(Lang::get('del success'), 'Manu/index');
    }

    /**
     * 简单的模糊搜索
     * @return mixed
     */
    public function search(){
        $search = input('param.search');
        $data = $this->manus()->searchLike($search);
        return $this->show($data);
        /*$this->page($data);
        $this->assign('manus', $data);
        return $this->fetch('manu/index');*/
    }




}